﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mvc;
using System.Net;

namespace Mailer.Controllers
{
	public class RawResult : ActionResult
	{
		public byte[] ResultBytes { get; set; }
		public string ContentType { get; set; }
		public string ResultString { get; set; }
		public HttpStatusCode StatusCode { get; set; }
		public string Location { get; set; }

		public RawResult()
		{
			ResultString = "";
			ContentType = "text/plain";
			ResultBytes = null;
			StatusCode = HttpStatusCode.OK;
			Location = "";
		}

		public RawResult(string data, string mimetype)
		{
			ResultString = data;
			ResultBytes = null;
			ContentType = mimetype;
			StatusCode = HttpStatusCode.OK;
			Location = "";
		}

		public RawResult(string data, string mimetype, HttpStatusCode status)
		{
			ResultString = data;
			ResultBytes = null;
			ContentType = mimetype;
			StatusCode = status;
			Location = "";
		}

		public RawResult(string data, string mimetype, HttpStatusCode status, string loc)
		{
			ResultString = data;
			ResultBytes = null;
			ContentType = mimetype;
			StatusCode = status;
			Location = loc;
		}

		public RawResult(byte[] data, string mimetype)
		{
			ResultString = "";
			ResultBytes = data;
			ContentType = mimetype;
			StatusCode = HttpStatusCode.OK;
			Location = "";
		}

		public override void ExecuteResult(ControllerContext context)
		{
			context.HttpContext.Response.ContentType = ContentType;
			
			if (!string.IsNullOrEmpty(Location))
			{
				context.HttpContext.Response.Redirect(Location);
			}

			if (ResultBytes != null)
			{
				context.HttpContext.Response.OutputStream.Write(ResultBytes, 0, ResultBytes.Length);
			}
			else
			{
				context.HttpContext.Response.Write(ResultString);
			}

		}
	}

}
